#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：partial_codes-master 
@File ：msg_sender.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2024/2/23 23:56 
"""
import datetime
import socket
import time
import random

HOST = '127.0.0.1'
PORT = 12345


def connect():
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	try:
		s.connect((HOST, PORT))
	except ConnectionRefusedError:
		print("Connection refused, retrying...")
		time.sleep(1)
		return None
	else:
		print("Connected!")
		return s


while True:
	socket_con = connect()
	if socket_con:
		# msg = input("Input msg:")
		# socket_con.sendall(msg.encode())

		msg_list = ['2sz', '2lt', '3dx', ]
		# 随机选择
		# msg = random.choice(msg_list)
		msg = '2sz'
		print("Sending msg:", msg)
		time.sleep(1)
		socket_con.sendall(msg.encode())
	else:
		# 连接失败则再次尝试连接
		pass
